import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.PriorityBlockingQueue;

public class runPriority {

    final static BlockingQueue<Task> queue = new PriorityBlockingQueue<Task>();
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(10);
        for(int i=0;i<10;i++){
            pool.execute(new Runnable() {
                @Override
                public void run() {
                    int priority = (int) (Math.random()*100);
                    String tno = "t"+Thread.currentThread().getId();
                    Task task = new Task(priority,tno);
                    try{
                        queue.put(task);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }
        while(queue.size()<10){
            System.out.println("现在队列里面的任务有"+queue.size()+"个");
        }
        new Thread(){
            public void run(){
                while(queue.size()>0){
                    try{
                        Task task = (Task) queue.take();
                        System.out.println("执行优先级为"+task.getPriority()+"的任务："+task.getTno());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                System.out.println("处理完成所有任务");
            }
        }.start();
        pool.shutdown();

    }
}
